var express = require("express");
var router = express.Router();
var db = require("../model/index");
/* 查询所有商品 */
router.get("/user_list/getlist", function (req, res, next) {
    let mysql = `SELECT * FROM user_list`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询角色信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询角色信息成功",
            data: data,
        });
    });
});
/* 用户列表删除角色 */
router.post("/user_list/getlist1", function (req, res, next) {
    let mysql = `DELETE FROM user_list WHERE id = '${req.body.id}';`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "删除角色失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "删除角色成功",
            data: data,
        });
    });
});
/* 启动禁用角色 */
router.post('/user_list/changeEnable', function (req, res, next) {
    let mysql = `UPDATE user_list SET enable='${req.body.enable}' WHERE id='${req.body.id}';`
    db(mysql, function (err, data) {
      if (err) {
        return res.json({
          code: 500,
          msg: '启用禁用角色失败，请稍后重试',
        })
      }
      res.json({
        code: 200,
        msg: '启用禁用角色成功',
        data: data
      })
    })
  });
// 用户列表编辑
  router.post("/user_list/ModifyData", function (req, res, next) {
    console.log(req.body);
    let mysql = `UPDATE  user_list SET name = ?,zhanghao= ?,shouji= ?,time=? ,jine = ? WHERE id = ?`;
    console.log("req", req.body);
    db(
        mysql,
        [
            req.body.name,
            req.body.zhanghao,
            req.body.shouji,
            req.body.jine,
            req.body.time,
            req.body.id,
        ],
        function (err, data) {
            console.log("err", err);
            if (err) {
                return res.json({
                    code: 500,
                    msg: "修改管理员信息失败，请稍后重试",
                });
            }
            res.json({
                code: 200,
                msg: "修改管理员信息成功",
                data: data,
            });
        }
    );
});
// 用户列表查看详情
router.post("/user_list/SeeList", function (req, res, next) {
    console.log(req.body);
    let mysql = `UPDATE  user_list SET name = ?,zhanghao= ?,shouji= ?,time= ?,jine= ? WHERE id = ?`;
    console.log("req", req.body);
    db(
        mysql,
        [
            req.body.name,
            req.body.zhanghao,
            req.body.shouji,
            req.body.time,
            req.body.jine,
            req.body.id,
        ],
        function (err, data) {
            console.log("err", err);
            if (err) {
                return res.json({
                    code: 500,
                    msg: "查看用户列表失败，请稍后重试",
                });
            }
            res.json({
                code: 200,
                msg: "查看用户列表成功",
                data: data,
            });
        }
    );
});

/* 批量删除用户列表信息 */
router.post("/user_list/Piliangshanchu", function (req, res, next) {
    let mysql = `DELETE FROM user_list WHERE `;
    for (let i = 0; i < req.body.length; i++) {
        mysql = mysql + `id=${req.body[i].id}` + " or ";
    }
    mysql = mysql.replace(/or $/g, "");
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "批量删除用户列表信息失败，请稍后重试",
                data: err,
            });
        }
        res.json({
            code: 200,
            msg: "批量删除用户列表信息成功",
            data: data,
        });
    });
});

// 用户列表信息的模糊查询
router.get("/user_list/ListSelect", function (req, res, next) {
    let mysql = `
    SELECT t1.id,t1.name,t1.zhanghao,t1.shouji,t1.time,t2.username,t1.jine
    FROM user_list AS t1 LEFT JOIN USER AS t2 ON t1.zhanghao=t2.id
    WHERE name LIKE '%${req.query.username}%'   AND zhanghao LIKE '%${req.query.search}%'
    AND time LIKE '%${req.query.riqi}%'`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询用户信息失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询用户信息成功",
            data: data,
        });
    });
});

// 查询所有标签
router.get("/user_list/getlabel", function (req, res, next) {
    let mysql = `SELECT * FROM user_label`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询用户标签失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询用户标签成功",
            data: data,
        });
    });
});
/* 删除标签 */
router.post("/user_list/getlabel1", function (req, res, next) {
    let mysql = `DELETE FROM user_label WHERE id = '${req.body.id}';`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "删除用户标签失败，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "删除用户标签成功",
            data: data,
        });
    });
});
// 用户标签编辑
router.post("/user_list/YonghuEdit", function (req, res, next) {
    console.log(req.body);
    let mysql = `UPDATE  user_label SET name = ?,renshu= ?,tiaojian= ? WHERE id = ?`;
    console.log("req", req.body);
    db(
        mysql,
        [
            req.body.name,
            req.body.renshu,
            req.body.tiaojian,
            req.body.id,
        ],
        function (err, data) {
            console.log("err", err);
            if (err) {
                return res.json({
                    code: 500,
                    msg: "修改用户标签失败，请稍后重试",
                });
            }
            res.json({
                code: 200,
                msg: "修改用户标签成功",
                data: data,
            });
        }
    );
});

// 添加用户标签
router.post('/user_list/addlabel', function (req, res, next) {
    let mysql = `INSERT INTO user_label VALUES(NULL,'${req.body.name}','${req.body.renshu}','${req.body.tiaojian}');`
    db(mysql, function (err, data) {
      if (err) {
        return res.json({
          code: 500,
          msg: '新增标签失败，请稍后重试',
          data: err
        })
      }
      res.json({
        code: 200,
        msg: '新增标签成功',
      })
    })
  });

// 购买力筛选获取数据
router.get("/user_list/getpower", function (req, res, next) {
    let mysql = `SELECT * FROM user_power`;
    db(mysql, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: "查询购买力筛选，请稍后重试",
            });
        }
        res.json({
            code: 200,
            msg: "查询购买力筛选成功",
            data: data,
        });
    });
});

// 购买力筛选查看
router.post("/user_list/SeePower", function (req, res, next) {
    console.log(req.body);
    let mysql = `UPDATE  user_power SET zhanghao = ?,xiaofei= ?,cishu= ?,junjia= ?,time= ? WHERE name = ?`;
    console.log("req", req.body);
    db(
        mysql,
        [
            req.body.zhanghao,
            req.body.xiaofei,
            req.body.cishu,
            req.body.junjia,
            req.body.time,
            req.body.name,
        ],
        function (err, data) {
            console.log("err", err);
            if (err) {
                return res.json({
                    code: 500,
                    msg: "查看购买力筛选失败，请稍后重试",
                });
            }
            res.json({
                code: 200,
                msg: "查看购买力筛选成功",
                data: data,
            });
        }
    );
});





module.exports = router;
